package com.zx.sms;

import com.zx.sms.codec.cmpp.msg.CmppSubmitRequestMessage;
import com.zx.sms.common.GlobalConstance;
import com.zx.sms.common.util.ChannelUtil;
import com.zx.sms.connect.manager.CMPPEndpointManager;
import com.zx.sms.connect.manager.EndpointEntity;
import com.zx.sms.connect.manager.cmpp.CMPPClientEndpointEntity;
import io.netty.channel.ChannelFuture;
import io.netty.util.concurrent.GenericFutureListener;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *经测试，35个连接，每个连接每200/s条消息
 *lenovoX250能承担7000/s消息编码解析无压力。
 *10000/s的消息服务不稳定，开个网页，或者打开其它程序导致系统抖动，会有大量消息延迟 (超过500ms)
 *
 *低负载时消息编码解码可控制在10ms以内。
 *
 */

/**
 * 模拟服务端向客户端发送消息
 */
public class ClientTestSendSubmitMsg {
	private static final Logger logger = LoggerFactory.getLogger(ClientTestSendSubmitMsg.class);

	@Test
	public void testCMPPEndpoint() throws Exception {

		while (true){
			EndpointEntity clientEndpointEntity = getClientEndpointEntity();

			CmppSubmitRequestMessage msg = CmppSubmitRequestMessage.create("1234","10086","中国移动");

			ChannelFuture future = ChannelUtil.asyncWriteToEntity(clientEndpointEntity, msg,new GenericFutureListener<ChannelFuture>() {
				@Override
				public void operationComplete(ChannelFuture future) throws Exception {
					if (null != future){
						System.out.println("短信发送，result="+future.isSuccess());
						Throwable throwable = future.cause();
						if (null != throwable){
							System.out.println("短信发送失败，");
							throwable.printStackTrace();
						}
					}
				}
			});

			Thread.currentThread().sleep(10000l);
		}


	}


	public static EndpointEntity getClientEndpointEntity() {
		CMPPClientEndpointEntity client = new CMPPClientEndpointEntity();
		client.setId("client1");
		client.setHost("127.0.0.1");
		client.setPort(7918);
		client.setChartset(GlobalConstance.defaultTransportCharset);
		//使用ssl加密数据流
		client.setGroupName("client");
		client.setUserName("901782");
		client.setPassword("ICP");


		client.setMaxChannels((short)12);
		client.setWindows((short)16);
		client.setVersion((short)0x30);
		client.setRetryWaitTimeSec((short) 1000);
		client.setUseSSL(false);
		client.setReSendFailMsg(true);

		return CMPPEndpointManager.INS.initEndpoint(client);
	}



}
